<template>
<div>
<el-row>

<el-form :inline="true" >
	<el-form-item >
		<daterange v-bind:se.sync="s_e" type="daterange"></daterange>
    </el-form-item>

	<el-form-item>
	    <el-button type="primary" @click="onSubmit" :loading="btnloading">查询</el-button>
	</el-form-item>

	<el-form-item>
	    <el-button type="success" @click="toExcel">导出Excel</el-button>
	</el-form-item>

</el-form>
</el-row>

<el-row>
<el-table
    :data="objs"
    border
    show-summary
    v-loading="loading"
    style="width: 100%">
    <el-table-column
      prop="name"
      label="产品名称"
      >
    </el-table-column>

    <el-table-column
      prop="attr_name"
      label="型号名称"
      width="300"
      >
    </el-table-column>

    <el-table-column
      prop="total_price"
      label="实际收入"
      width="120"
      sortable
      >
    </el-table-column>

    <el-table-column
      prop="one_price"
      label="一级"
      width="80"
      sortable
      >
    </el-table-column>

    <el-table-column
      prop="two_price"
      label="二级"
      width="80"
      sortable
      >
    </el-table-column>

    <el-table-column
      prop="profit_wb"
      label="分销其他"
      width="120"
      sortable
      >
    </el-table-column>

    <el-table-column
      prop="sxf_price"
      label="微信费用"
      width="120"
      sortable
      >
    </el-table-column>

    <!-- <el-table-column
      prop="sys_price"
      label="系统分佣"
      width="80"
      >
    </el-table-column> -->
    
    
</el-table>
</el-row>

</div>
</template>

<script>

import { mapGetters } from 'vuex'
import { date2str, dateDiff } from '../../../config/util'
import daterange from '../../../components/daterange'

export default {
	
	name : 'income',
	
	data () {
		return {
		  	s_e : [date2str(new Date()), date2str(new Date())],
		  	loading : false,
		  	filename : '',

		  	btnloading : false,
		}
	},

	props : {

	},

	components : {
		daterange,
	},

	computed : {
		...mapGetters([
			'income',
			'userInfo',
		]),

		objs () {
			let statistics = {};
			let arr = [];

			const length = this.income.length
			for (let i = 0; i < length; i++){
				let tmp = this.income[i];
				const key = tmp.sale_code + '-' + tmp.attr_id;
				if(statistics[key] == undefined){
					statistics[key] = {
                        'name' : tmp.title,
                        'attr_name' : tmp.attr_name,
                        'sale_code' : tmp.sale_code,
                        'attr_id' : tmp.attr_id,
                        'total_price' : tmp.total_price,    //实际收入
                        //'pay_fee' : tmp.pay_fee,            //支付金额 
                        'one_price' : tmp.one_price,        //一级分佣
                        'two_price' : tmp.two_price,        //二级分佣
                        'profit_wb' : tmp.profit_wb,        //分销他人产品成本
                        'sxf_price' : tmp.sxf_price,        //微信手续费
                        'sys_price' : tmp.sys_price,        //系统分佣
                        'voucher_price' : tmp.voucher_price,    //优惠券金额
                    };
				}else{
					statistics[key].total_price += tmp.total_price;
                    statistics[key].one_price += tmp.one_price;
                    statistics[key].two_price += tmp.two_price;
                    statistics[key].profit_wb += tmp.profit_wb;
                    statistics[key].sxf_price += tmp.sxf_price;
                    statistics[key].sys_price += tmp.sys_price;
                    statistics[key].voucher_price += tmp.voucher_price;
				}
			}

            for (let key1 in statistics) {
				for (let key2 in statistics[key1]) {
					if(typeof statistics[key1][key2] == 'number'){
						statistics[key1][key2] = Math.round(statistics[key1][key2] * 100) / 100;
					}
				}
				arr.push(statistics[key1]);
			}

			console.log(arr);

            return arr;
		},
		
	},

	methods : {
		getData() {

			const day = dateDiff(this.s_e[0], this.s_e[1]);
			
			if(day > 30){
                this.$message.warning('时间跨度不能超过30天');
                return;
			}

			this.loading = true;
			this.btnloading = true;

			this.filename = '收入报表' + '[' + this.s_e[0] + '~' + this.s_e[1] + ']';

			let para = {
				fun : 'income',
				start_time : this.s_e[0] + ' 00:00:00',
				end_time : this.s_e[1] + ' 23:59:59',
				cb : res => {
					this.loading = false;
					this.btnloading = false;
				},
				err : res => {
					this.loading = false;
					this.btnloading = false;
					this.$notify.error({
			          title: '错误',
			          message: res,
			        });
				}
			};
			this.$store.dispatch('query', para);
		},

		onSubmit () {
			this.getData();
		},

		toExcel (){
			
			let labels = ['code', 'title', 'attr_name', 'create_time', 'hexiao_time', 
			'name', 'mobile', 'cardno', 'uid_nickname', 'seller_remark', 
			'guide_price', 'num', 'des_num', 'total_guide',
			'pay_fee', 'total_price', 'nickname', 'one_price', 'nickname1', 'two_price', 'profit_wb', 
			'sxf_price', 'sys_price', 'voucher_price', 'remark'];
	        let arr = [];
	        for(let i = 0; i< this.income.length; i++){
	            let tmp = this.income[i];
	            let haha = [];
	            for(let j = 0; j < labels.length; j++){
	                haha.push(tmp[labels[j]]);
	            }

	            if(tmp.myproduct == 1 && tmp.sale_belong == this.userInfo.company_code){
	                haha.push('群盟');
	            }else{
	                haha.push('自己');
	            }

	            arr.push(haha);
	        }
	        arr.splice(0, 0, ['订单编号', '订单名称', '型号名称', '下单时间' , '分佣时间', 
	        	'姓名', '电活', '身份证', '下单人微信昵称', '卖家备注', 
	        	'单价', '购买数量', '核销数量', '总价', 
	        	'支付金额', '实际收入', '一级昵称', '一级分佣', '二级昵称', '二级分佣', '分销他人产品成本' ,
	        	'微信手续费', '系统分佣', '优惠券金额', '备注', '收入来自']);

	        ExportUtil.toExcel(arr, this.filename);

		}
	},

	watch : {
	    
	},

	mounted () {
		this.getData();
	},

}
</script>

<style scoped>

</style>
